package com.example.intermingle.config;

import com.example.intermingle.vo.User;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.authentication.AuthenticationServiceException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.session.SessionRegistry;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 绿松石
 * @version V1.0
 * @Title LoginFilterConfig 拦截校验
 * @Description
 * @date 2021年08月20日 13:38
 * @since V1.0
 */
public class LoginFilterConfig extends UsernamePasswordAuthenticationFilter {

    @Autowired
    SessionRegistry sessionRegistry;

    @Override
    public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException {
        //判断是否是post请求
        if (!request.getMethod().equals("POST")) {
            throw new AuthenticationServiceException(
                    "Authentication method not supported: " + request.getMethod());
        }
        //判断数据的提交格式
        if (request.getContentType().contains(MediaType.APPLICATION_JSON_VALUE) || request.getContentType().contains(MediaType.APPLICATION_JSON_UTF8_VALUE)) {
            Map<String, String> loginData = new HashMap<>();
            try {
                //获取用户登陆信息
                loginData = new ObjectMapper().readValue(request.getInputStream(), Map.class);
            } catch (IOException e) {

            }
            //获取账号信息
            String userCode = loginData.get(getUsernameParameter());
            //获取密码信息
            String userPassword = loginData.get(getPasswordParameter());
            //校验用户账号是否为空
            if (userCode.equals(null)&&userCode.equals("")) {
              throw new UsernameNotFoundException("账号不能为空");
            }
            if (userPassword.equals(null)&&userPassword.equals("")) {
                throw new UsernameNotFoundException("账号不能为空");
            }
            //账号密码去空
            userCode = userCode.trim();
            userPassword=userPassword.trim();
            //验证账号密码
            UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(
                    userCode, userPassword);
            setDetails(request, authRequest);
            //定义一个user对象
            User principal = new User();
            principal.setUserCode(userCode);
            //注册一个新session
            sessionRegistry.registerNewSession(request.getSession(true).getId(), principal);
            //返回身份认证
            return this.getAuthenticationManager().authenticate(authRequest);
        } else {
            return super.attemptAuthentication(request, response);
        }
    }
}
